import actionTypes from './actionTypes'
// import Storage from '../../utils/storage'

// 登录失败
export const loginFailed = () => {
  // Storage.set('authToken', '')
  // Storage.set('userInfo', null)

  return {
    type: actionTypes.LOGIN_FAILED
  }
}

// 开始登录
export const startLogin = () => ({
  type: actionTypes.START_LOGIN
})

// 开始登录
export const loginSuccess = (userInfo) => {
  return {
    type: actionTypes.LOGIN_SUCCESS,
    payload: userInfo
  }
}

// 登录
export const login = userInfo => dispatch => {
  // dispatch(startLogin())
  // console.log('action', userInfo)
  
  // todo 调用真正的登录接口
  // ........

  let user = {
    username: userInfo.username,
    nickname: 'Tom',
    age: 20,
    avatar: 'https://reactnative.dev/img/tiny_logo.png',
    authToken: 'test'+Math.random()
  }
  dispatch(loginSuccess(user))

  // 持久化存储
  // Storage.set('authToken', user.authToken)
  // Storage.set('userInfo', user)

}

// 退出
export const logout = () => dispatch => {
  // alert('用户退出')
  dispatch(loginFailed())
}

